package main

import "fmt"

//多态 断言实际应用
type Usb interface {
	Start()
	Stop()
}
type Phone struct {
	name string
}

func (p Phone) Start() {
	fmt.Println("手机开始充电" + p.name)
}

func (p Phone) Stop() {
	fmt.Println("手机停止充电" + p.name)
}
func (p Phone) call() {
	fmt.Println("手机开始打电话" + p.name)
}

type Camera struct {
	name string
}

func (c Camera) Start() {
	fmt.Println("像机开始充电" + c.name)
}

func (c Camera) Stop() {
	fmt.Println("相机停止充电" + c.name)
}

type Computer struct {
}

func (c Computer) working(usb Usb) {
	usb.Start()
	//如果usb指向phone结构体变量，还需要调用call()
	//类型断言
	if phone, ok := usb.(Phone); ok {
		phone.call()
	}
	usb.Stop()
}
func main() {
	var computer Computer
	var usbArr [3]Usb
	usbArr[0] = Phone{"iPhone"}
	usbArr[1] = Camera{"佳能"}
	usbArr[2] = Phone{"华为"}
	for _, i2 := range usbArr {
		computer.working(i2)
	}
}
